package com.paishen.peiniwan.frame.http;

import android.text.TextUtils;

import com.paishen.common.okhttp.OkCallbackAdapter;
import com.paishen.common.okhttp.OkHttpClientManager;
import com.paishen.common.okhttp.OkProgressHelper;
import com.paishen.peiniwan.App;
import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
import com.squareup.okhttp.Headers;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.MultipartBuilder;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;

import java.io.File;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

/**
 * Http交互管理器
 * Created by peng on 2015/12/2.
 */
public class PnwHttpManager {
    public static final String S_OFFLINE = "http://test.bzihao.com";
    public static String SERVICE = S_OFFLINE;
    public static String URL_SERVICE = SERVICE + "/index.php/API";
    public static String URL_SERVICE_PAY_NOTIFY = SERVICE + "/index.php/API/Order/alipay_notify";
    public static String URL_SERVICE_PAY_EXTRACT = SERVICE + "/index.php/HTML5/Pay/login";
    public static String URL_SERVICE_SHARE = SERVICE + "/index.php/HTML5/Share?oid=";
    public static String URL_SERVICE_LOGO = SERVICE + "/Public/system/logo.png";

    public static void setCurrServiceUrl(String switchUrl) {
        if (!TextUtils.isEmpty(switchUrl)) {
            SERVICE = switchUrl;
            URL_SERVICE = SERVICE + "/index.php/API";
            URL_SERVICE_PAY_NOTIFY = SERVICE + "/index.php/API/Order/alipay_notify";
            URL_SERVICE_PAY_EXTRACT = SERVICE + "/index.php/HTML5/Pay/login";
            URL_SERVICE_SHARE = SERVICE + "/index.php/HTML5/Share?oid=";
            URL_SERVICE_LOGO = SERVICE + "/Public/system/logo.png";
        }
    }

    /**
     * 用户模块
     */
    public static class User {
        static String URL_MODULE_USER = "/User";

        /**
         * 登录 [phone+pwd] or （废弃） [third_type+openid]
         *
         * @param phone             必填 用户手机号码
         * @param pwd               必填 如果为陪你玩用户则输入密码
         * @param okCallbackAdapter 响应回调
         */
        public static void login(String tag, String phone, String pwd, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/login";
            Map<String, String> params = new HashMap<>();
            params.put("phone", phone);
            params.put("pwd", pwd);

            final String jPushRegistrationId = App.i().getJPushRegistrationId();
            if (!TextUtils.isEmpty(jPushRegistrationId)) {
                params.put("channel_id", jPushRegistrationId);
            }
            params.put("os", "Android");

            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取验证码短信根据手机号码
         *
         * @param mobile            必填 手机号码
         * @param type              必填 类型 [reg|forget]
         * @param okCallbackAdapter 响应回调
         */
        public static void sendValidCode(String tag, String mobile, String type, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/send_valid_code";
            Map<String, String> params = new HashMap<>();
            params.put("mobile", mobile);
            params.put("type", type);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 检验验证码
         *
         * @param mobile            必填 手机号码
         * @param okCallbackAdapter 响应回调
         */
        public static void checkValidCode(String tag, String mobile, String vcode, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/check_valid_code";
            Map<String, String> params = new HashMap<>();
            params.put("mobile", mobile);
            params.put("vcode", vcode);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 补全用户注册的信息昵称，头像，性别等
         *
         * @param nickname          必填 昵称
         * @param photo             必填 头像
         * @param sex               必填 男|女
         * @param age               必填 年龄
         * @param city              必填 居住地 格式[XX-XX] 例如[湖北-武汉]
         * @param salary            必填 月薪
         * @param password          必填 密码
         * @param inviteCode        选填 邀请码
         * @param platform          必填 android 渠道平台
         * @param okCallbackAdapter 响应回调
         */
        public static void completeReg(String tag, String nickname, File photo, int sex, String age,
                                       String city, String salary, String password, String inviteCode, String platform, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/complete_reg";
            Map<String, String> params = new HashMap<>();
            params.put("nickname", nickname);
            params.put("sex", String.valueOf(sex));
            params.put("age", age);
            params.put("city", city);
            params.put("salary", salary);
            params.put("password", password);
            if (!TextUtils.isEmpty(inviteCode)) params.put("inviteCode", inviteCode);

            final String jPushRegistrationId = App.i().getJPushRegistrationId();
            if (!TextUtils.isEmpty(jPushRegistrationId)) {
                params.put("channel_id", jPushRegistrationId);
            }
            params.put("os", "Android");
            params.put("platform", platform);


            MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
            for (String key : params.keySet()) {
                builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                        RequestBody.create(null, params.get(key)));
            }

            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String contentTypeFor = fileNameMap.getContentTypeFor(photo.getAbsolutePath());
            if (contentTypeFor == null) contentTypeFor = "application/octet-stream";


            builder.addPart(Headers.of("Content-Disposition",
                            "form-data; name=\"" + "photo" + "\"; filename=\"" + photo.getName() + "\""),
                    RequestBody.create(MediaType.parse(contentTypeFor), photo));
            RequestBody requestBody = builder.build();

            final Request request = new Request.Builder().url(url).post(OkProgressHelper.addProgressRequestListener(requestBody, new OkCallbackAdapter<String>() {
                @Override
                public void onProgress(final long bytesWrite, final long contentLength, final boolean done) {
                    OkHttpClientManager.getInstance().mDelivery.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallbackAdapter.onProgress(bytesWrite, contentLength, done);
                        }
                    });
                }
            })).tag(tag).header(OkHttpClientManager.USER_AGENT, App.i().getUserAgent()).build();

            OkHttpClientManager.getInstance().request(url, request, okCallbackAdapter);
        }

        /**
         * 忘记密码
         *
         * @param vcode             必填 验证码
         * @param mobile            必填 手机号
         * @param pwd               必填 密码
         * @param okCallbackAdapter 响应回调
         */
        public static void forgetPassword(String tag, String vcode, String mobile, String pwd, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/forget_password";
            Map<String, String> params = new HashMap<>();
            params.put("vcode", vcode);
            params.put("mobile", mobile);
            params.put("pwd", pwd);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 修改密码
         *
         * @param old_pwd           必填 原密码
         * @param new_pwd           必填 新密码
         * @param okCallbackAdapter 响应回调
         */
        public static void changePassword(String tag, String old_pwd, String new_pwd, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/change_password";
            Map<String, String> params = new HashMap<>();
            params.put("old_pwd", old_pwd);
            params.put("new_pwd", new_pwd);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 申请陪你玩认证
         *
         * @param real_photo        必填 真实头像
         * @param okCallbackAdapter 响应回调
         */
        public static void validApplication(String tag, File real_photo, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/valid_application";
            Map<String, String> params = new HashMap<>();

            MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
            for (String key : params.keySet()) {
                builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                        RequestBody.create(null, params.get(key)));
            }

            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            String contentTypeFor = fileNameMap.getContentTypeFor(real_photo.getAbsolutePath());
            if (contentTypeFor == null) contentTypeFor = "application/octet-stream";


            builder.addPart(Headers.of("Content-Disposition",
                            "form-data; name=\"" + "real_photo" + "\"; filename=\"" + real_photo.getName() + "\""),
                    RequestBody.create(MediaType.parse(contentTypeFor), real_photo));
            RequestBody requestBody = builder.build();

            final Request request = new Request.Builder().url(url).post(OkProgressHelper.addProgressRequestListener(requestBody, new OkCallbackAdapter<String>() {
                @Override
                public void onProgress(final long bytesWrite, final long contentLength, final boolean done) {
                    OkHttpClientManager.getInstance().mDelivery.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallbackAdapter.onProgress(bytesWrite, contentLength, done);
                        }
                    });
                }
            })).tag(tag).header(OkHttpClientManager.USER_AGENT, App.i().getUserAgent()).build();

            OkHttpClientManager.getInstance().request(url, request, okCallbackAdapter);
        }

        /**
         * 获取陪你玩认证信息
         *
         * @param okCallbackAdapter 响应回调
         */
        public static void validInfo(String tag, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/valid_info";
            Map<String, String> params = new HashMap<>();
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 用户信息
         *
         * @param id                必填 用户ID
         * @param okCallbackAdapter 响应回调
         */
        public static void info(String tag, long id, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/info";
            Map<String, String> params = new HashMap<>();
            params.put("id", String.valueOf(id));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
            okCallbackAdapter.setCache(tag, url, params);
        }

        /**
         * 修改个人资料
         *
         * @param nickname          必填 昵称
         * @param intro             选填 个人简介
         * @param tags              选填 标签
         * @param photo             必填 头像
         * @param height            必填 身高
         * @param weight            必填 体重
         * @param age               必填 年龄
         * @param profession        选填 职业
         * @param degree            选填 选填
         * @param constellation     选填 星座
         * @param city              选填 地点
         * @param salary            选填 月薪
         * @param okCallbackAdapter 响应回调
         */
        public static void updateProfile(String tag, String nickname, String intro, String tags, File photo, String height, String weight
                , String age, String profession, String degree, String constellation, String city, String salary, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/update_profile";
            Map<String, String> params = new HashMap<>();
            params.put("nickname", nickname);
            params.put("intro", intro);
            params.put("tags", tags);

            params.put("salary", salary);
            params.put("age", age);
            params.put("city", city);
            params.put("constellation", constellation);
            params.put("degree", degree);
            params.put("height", height);
            params.put("profession", profession);
            params.put("weight", weight);


            MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
            for (String key : params.keySet()) {
                builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                        RequestBody.create(null, params.get(key)));
            }

            if (photo != null) {
                FileNameMap fileNameMap = URLConnection.getFileNameMap();
                String contentTypeFor = fileNameMap.getContentTypeFor(photo.getAbsolutePath());
                if (contentTypeFor == null) contentTypeFor = "application/octet-stream";

                builder.addPart(Headers.of("Content-Disposition",
                                "form-data; name=\"" + "photo" + "\"; filename=\"" + photo.getName() + "\""),
                        RequestBody.create(MediaType.parse(contentTypeFor), photo));
            }

            RequestBody requestBody = builder.build();

            final Request request = new Request.Builder().url(url).post(OkProgressHelper.addProgressRequestListener(requestBody, new OkCallbackAdapter<String>() {
                @Override
                public void onProgress(final long bytesWrite, final long contentLength, final boolean done) {
                    OkHttpClientManager.getInstance().mDelivery.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallbackAdapter.onProgress(bytesWrite, contentLength, done);
                        }
                    });
                }
            })).tag(tag).header(OkHttpClientManager.USER_AGENT, App.i().getUserAgent()).build();

            OkHttpClientManager.getInstance().request(url, request, okCallbackAdapter);
        }

        /**
         * 获取相册照片
         *
         * @param uid               必填 用户ID
         * @param okCallbackAdapter 响应回调
         */
        public static void getPhoto(String tag, long uid, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/get_photo";
            Map<String, String> params = new HashMap<>();
            params.put("uid", String.valueOf(uid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
            okCallbackAdapter.setCache(tag, url, params);
        }

        /**
         * 删除个人相册
         *
         * @param pid               必填  删除的照片id
         * @param okCallbackAdapter 响应回调
         */
        public static void deletePhoto(String tag, Long[] pid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/delete_photo";
            Map<String, String> params = new HashMap<>();

            StringBuilder sbPid = new StringBuilder();
            for (long i : pid) {
                sbPid.append(i).append(",");
            }
            params.put("pid", sbPid.toString());

            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 上传相册照片
         *
         * @param photos            必填 要上传的照片
         * @param okCallbackAdapter 响应回调
         */
        public static void uploadPhoto(String tag, File[] photos, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/upload_photo";
            Map<String, String> params = new HashMap<>();

            MultipartBuilder builder = new MultipartBuilder().type(MultipartBuilder.FORM);
            for (String key : params.keySet()) {
                builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + key + "\""),
                        RequestBody.create(null, params.get(key)));
            }

            if (photos != null) {
                FileNameMap fileNameMap = URLConnection.getFileNameMap();
                for (int i = 0; i < photos.length; i++) {
                    String contentTypeFor = fileNameMap.getContentTypeFor(photos[i].getAbsolutePath());
                    if (contentTypeFor == null) contentTypeFor = "application/octet-stream";

                    builder.addPart(Headers.of("Content-Disposition",
                                    "form-data; name=\"" + "photo_" + i + "\"; filename=\"" + photos[i].getName() + "\""),
                            RequestBody.create(MediaType.parse(contentTypeFor), photos[i]));
                }
            }

            RequestBody requestBody = builder.build();

            final Request request = new Request.Builder().url(url).post(OkProgressHelper.addProgressRequestListener(requestBody, new OkCallbackAdapter<String>() {
                @Override
                public void onProgress(final long bytesWrite, final long contentLength, final boolean done) {
                    OkHttpClientManager.getInstance().mDelivery.post(new Runnable() {
                        @Override
                        public void run() {
                            okCallbackAdapter.onProgress(bytesWrite, contentLength, done);
                        }
                    });
                }
            })).tag(tag).header(OkHttpClientManager.USER_AGENT, App.i().getUserAgent()).build();

            OkHttpClientManager.getInstance().request(url, request, okCallbackAdapter);
        }


        /**
         * 更新用户地标
         *
         * @param lat               必填 纬度
         * @param lng               必填 经度
         * @param okCallbackAdapter 响应回调
         */
        public static void updateLandmark(String tag, double lat, double lng, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/update_landmark";
            Map<String, String> params = new HashMap<>();
            params.put("lat", String.valueOf(lat));
            params.put("lng", String.valueOf(lng));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 根据环信帐号获取用户信息
         *
         * @param easid             环信帐号
         * @param okCallbackAdapter
         */
        public static void getUserByEasemob(String tag, String[] easid, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/getUserByEasemob";
            Map<String, String> params = new HashMap<>();

            for (int i = 0; i < easid.length; i++) {
                params.put("easid[" + i + "]", easid[i]);
            }

            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 检查收藏
         *
         * @param uid               必填 目标ID
         * @param okCallbackAdapter 响应回调
         */
        public static void checkCollected(String tag, long uid, final OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/check_collected";
            Map<String, String> params = new HashMap<>();
            params.put("uid", String.valueOf(uid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取收藏列表
         *
         * @param lastid            选填 最后ID
         * @param okCallbackAdapter 响应回调
         */
        public static void collectList(String tag, long lastid, final PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/collect_list";
            Map<String, String> params = new HashMap<>();
            if (lastid > 0) params.put("lastid", String.valueOf(lastid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
            okCallbackAdapter.setCache(tag, url, params);
        }

        /**
         * 获取粉丝列表
         *
         * @param lastid            选填 最后ID
         * @param okCallbackAdapter 响应回调
         */
        public static void fansList(String tag, long lastid, final PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/fans_list";
            Map<String, String> params = new HashMap<>();
            if (lastid > 0) params.put("lastid", String.valueOf(lastid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
            okCallbackAdapter.setCache(tag, url, params);
        }

        /**
         * 获取陪丸数目，关注数目，粉丝数目，我的约会数目，是否真人认证
         *
         * @param okCallbackAdapter 响应回调
         */
        public static void miscInfo(String tag, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/miscInfo";
            Map<String, String> params = new HashMap<>();

            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 校验手机号码及邀请注册码
         *
         * @param phone             必填 手机号
         * @param inviteCode        选填 邀请注册码
         * @param okCallbackAdapter 响应回调
         */
        public static void checksumPhoneAndInviteCode(String tag, String phone, String inviteCode, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_USER + "/checksumPhoneAndInviteCode";
            Map<String, String> params = new HashMap<>();
            params.put("phone", phone);
            if (!TextUtils.isEmpty(inviteCode)) params.put("inviteCode", inviteCode);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    /**
     * 反馈模块
     */
    public static class Feedback {
        static String URL_MODULE_FEEDBACK = "/Feedback";

        /**
         * 添加反馈
         *
         * @param title             必填 标题
         * @param content           必填 内容
         * @param nickname          必填 昵称
         * @param okCallbackAdapter 响应回调
         */
        public static void add(String tag, String title, String content, String nickname, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_FEEDBACK + "/add";
            Map<String, String> params = new HashMap<>();
            params.put("title", title);
            params.put("content", content);
            params.put("nickname", nickname);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    public static class Coupon {
        static String URL_MODULE_COUPON = "/Coupon";

        /**
         * 添加优惠券
         *
         * @param code              必填 优惠码
         * @param okCallbackAdapter 响应回调
         */
        public static void add(String tag, String code, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_COUPON + "/add";
            Map<String, String> params = new HashMap<>();
            params.put("code", code);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取我的优惠券
         *
         * @param is_valid          必填 true/false [true表示有效优惠券#false表示全部优惠券]
         * @param okCallbackAdapter 响应回调
         */
        public static void get(String tag, boolean is_valid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_COUPON + "/get";
            Map<String, String> params = new HashMap<>();
            params.put("is_valid", String.valueOf(is_valid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    public static class Order {
        static String URL_MODULE_ORDER = "/Order";

        /**
         * 我的下单
         *
         * @param last              选填 最后一个订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void getOrder(String tag, long last, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/get_order";
            Map<String, String> params = new HashMap<>();
            if (last > 0) params.put("last", String.valueOf(last));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 我的抢单
         *
         * @param last              选填 最后一个订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void getBook(String tag, long last, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/get_book";
            Map<String, String> params = new HashMap<>();
            if (last > 0) params.put("last", String.valueOf(last));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 下单
         *
         * @param subject_type      必填 主题内容 例[运动]
         * @param date_from         必填 起始时间 yyyy-MM-dd HH:mm
         * @param address           必填 地址
         * @param lat               必填 纬度
         * @param lng               必填 经度
         * @param sex               必填 约炮对象性别 [0不限#1男#2女]
         * @param gift_id           选填 礼物ID
         * @param okCallbackAdapter 响应回调
         */
        public static void create(String tag, String subject_type, String date_from, String address, double lat, double lng, int sex, long gift_id, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/create";
            Map<String, String> params = new HashMap<>();
            params.put("subject_type", subject_type);
            params.put("date_from", date_from);
            params.put("address", address);
            params.put("lat", String.valueOf(lat));
            params.put("lng", String.valueOf(lng));
            params.put("sex", String.valueOf(sex));

            if (gift_id > 0) params.put("gift_id", String.valueOf(gift_id));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 下单单约
         *
         * @param targetID          必填 对方环信id
         * @param subject_type      必填 主题内容 例[运动]
         * @param date_from         必填 起始时间 yyyy-MM-dd HH:mm
         * @param address           必填 地址
         * @param lat               必填 纬度
         * @param lng               必填 经度
         * @param sex               必填 约炮对象性别 [0不限#1男#2女]
         * @param gift_id           选填 礼物ID
         * @param okCallbackAdapter 响应回调
         */
        public static void createSingle(String tag, String targetID, String subject_type, String date_from, String address, double lat, double lng, int sex, long gift_id, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/createSingle";
            Map<String, String> params = new HashMap<>();
            params.put("subject_type", subject_type);
            params.put("date_from", date_from);
            params.put("address", address);
            params.put("lat", String.valueOf(lat));
            params.put("lng", String.valueOf(lng));
            params.put("sex", String.valueOf(sex));
            params.put("gift_id", String.valueOf(gift_id));
            params.put("targetID", targetID);

            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);

        }

        /**
         * 订单详情
         *
         * @param oid               必填 订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void info(String tag, long oid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/info";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }


        /**
         * 待抢单
         *
         * @param p                 选填 页面编号 第一页为1
         * @param okCallbackAdapter 响应回调
         */
        public static void getBookPending(String tag, int p, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/get_book_pending";
            Map<String, String> params = new HashMap<>();
            if (p > 0) params.put("p", String.valueOf(p));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 抢单
         *
         * @param oid               必填 订单ID
         * @param gift_id           选填 礼物id (当create中的gift_id为空时当前接口中的gift_id为必传项)
         * @param okCallbackAdapter 响应回调
         */
        public static void book(String tag, long oid, long gift_id, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/book";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            if (gift_id > 0) params.put("gift_id", String.valueOf(gift_id));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 撤销订单
         *
         * @param oid               必填 订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void repeal(String tag, long oid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/repeal";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

//        /**
//         * 获取近期订单数量
//         *
//         * @param okCallbackAdapter 响应回调
//         */
//        @Deprecated
//        public static void getRecentOrder(String tag, OkCallbackAdapter okCallbackAdapter) {
//            String url = URL_SERVICE + URL_MODULE_ORDER + "/get_recent_order";
//            Map<String, String> params = new HashMap<>();
//            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
//        }

        /**
         * 选择抢单人
         *
         * @param oid               必填 订单ID
         * @param bid               必填 抢单者ID
         * @param okCallbackAdapter 响应回调
         */
        public static void selectUser(String tag, long oid, long bid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/select_user";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            params.put("bid", String.valueOf(bid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

//        /**
//         * 确认取消订单
//         *
//         * @param oid               必填 订单ID
//         * @param bid               必填 应约订单ID
//         * @param reason            必填 取消性质
//         * @param okCallbackAdapter 响应回调
//         */
//        @Deprecated
//        public static void cancelled(String tag, long oid, long bid, int reason, OkCallbackAdapter okCallbackAdapter) {
//            String url = URL_SERVICE + URL_MODULE_ORDER + "/cancelled";
//            Map<String, String> params = new HashMap<>();
//            params.put("oid", String.valueOf(oid));
//            params.put("bid", String.valueOf(bid));
//            params.put("reason", String.valueOf(reason));
//            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
//        }

        /**
         * 取消订单
         *
         * @param oid               必填 订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void cancelOrder(String tag, long oid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/cancelOrder";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 接受单约
         *
         * @param oid               必填 订单ID
         * @param type              必填                           1接受单约 0回绝单约
         * @param okCallbackAdapter 响应回调
         */
        public static void modifySingle(String tag, long oid, int type, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/modifySingle";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            params.put("type", String.valueOf(type));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }


        /**
         * 账户余额支付
         *
         * @param oid               必填 订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void payOrderWithBalance(String tag, long oid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/payOrderWithBalance";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 确认订单
         *
         * @param oid               必填 订单ID
         * @param okCallbackAdapter 响应回调
         */
        public static void confirmOrder(String tag, long oid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/confirmOrder";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 送礼物
         *
         * @param sendToEaseID      必填 礼物接收方环信ID
         * @param giftID            必填 礼物ID
         * @param okCallbackAdapter 响应回调
         */
        public static void sendGift(String tag, String sendToEaseID, long giftID, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/sendGift";
            Map<String, String> params = new HashMap<>();
            params.put("sendToEaseID", sendToEaseID);
            params.put("giftID", String.valueOf(giftID));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 充值
         *
         * @param rechargeCount     充值数额
         * @param okCallbackAdapter 响应回调
         */
        public static void recharge(String tag, int rechargeCount, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_ORDER + "/recharge";
            Map<String, String> params = new HashMap<>();
            params.put("rechargeCount", String.valueOf(rechargeCount));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    public static class Wallet {
        static String URL_MODULE_WALLET = "/Wallet";

        /**
         * 查看余额
         *
         * @param okCallbackAdapter 响应回调
         */
        public static void get(String tag, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_WALLET + "/get";
            Map<String, String> params = new HashMap<>();
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取收支明细
         *
         * @param p                 选填 分页
         * @param okCallbackAdapter 响应回调
         */
        public static void getBalanceDetail(String tag, int p, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_WALLET + "/get_balance_detail";
            Map<String, String> params = new HashMap<>();
            if (p > 0) params.put("p", String.valueOf(p));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取提现记录
         *
         * @param p                 选填 分页
         * @param okCallbackAdapter 响应回调
         */
        public static void getBalanceRecord(String tag, int p, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_WALLET + "/get_balance_record";
            Map<String, String> params = new HashMap<>();
            if (p > 0) params.put("p", String.valueOf(p));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

    }


    public static class Comment {
        static String URL_MODULE_COMMENT = "/Comment";

        /**
         * 添加评论
         *
         * @param oid               必填 订单ID
         * @param score             必填 分数
         * @param content           必填 内容
         * @param okCallbackAdapter 响应回调
         */
        public static void add(String tag, long oid, float score, String content, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_COMMENT + "/add";
            Map<String, String> params = new HashMap<>();
            params.put("oid", String.valueOf(oid));
            params.put("score", String.valueOf(score));
            params.put("content", content);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        /**
         * 获取评论
         *
         * @param uid               必填 用户ID
         * @param lid               选填 最后一个评论ID
         * @param okCallbackAdapter 响应回调
         */
        public static void get(String tag, long uid, long lid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_COMMENT + "/get";
            Map<String, String> params = new HashMap<>();
            params.put("uid", String.valueOf(uid));
            if (lid > 0) params.put("lid", String.valueOf(lid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }


    public static class Token {
        static String URL_MODULE_TOKEN = "/Token";

        public static int UPGRADE = 101;

        public static void up(String tag, int ver, String platform, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_TOKEN + "/up";
            Map<String, String> params = new HashMap<>();
            params.put("version", String.valueOf(ver));
            params.put("os", "Android");
            params.put("platform", platform);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }

        public static void getServer(String tag, int ver, String platform, OkCallbackAdapter okCallbackAdapter) {
            String url = "http://test.bzihao.com/index.php/API/Token/getServer";
            Map<String, String> params = new HashMap<>();
            params.put("version", String.valueOf(ver));
            params.put("os", "Android");
            params.put("platform", platform);
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    public static void cancel(String tag) {
        OkHttpClientManager.getInstance().cancel(tag);
    }


    public static class Square {
        public static String URL_MODULE_SQUARE = "/Square";

        //排序规则 [recently] 附近
        public static final String SQUARE_FILTER_TYPE_RECENTLY = "recently";
        // 排序规则 [recentlogin] 最近
        public static final String SQUARE_FILTER_TYPE_RECENT_LOGIN = "recentlogin";
        public static final int SQUARE_FILTER_GENDER_MALE = 1;
        public static final int SQUARE_FILTER_GENDER_FEMALE = 2;
        public static final int SQUARE_FILTER_GENDER_UNLIMITED = 0;

        /**
         * 获取广场首页
         *
         * @param sex               选填 筛选性别 [男|女|全部]
         * @param order             选填 排序规则 [recently] 附近 [recentlogin] 最近
         * @param okCallbackAdapter 响应回调
         */
        public static void index(String tag, int sex, String order, int p, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_SQUARE + "/index";
            Map<String, String> params = new HashMap<>();
            params.put("condition[sex]", String.valueOf(sex));
            params.put("condition[order]", order);
            if (p > 0) params.put("p", String.valueOf(p));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);

            if (p == 1 && TextUtils.equals(order, SQUARE_FILTER_TYPE_RECENT_LOGIN))
                okCallbackAdapter.setCache(tag, url, params).takeOutCache();
        }

        /**
         * 获取广场Banner
         *
         * @param okCallbackAdapter 响应回调
         */
        public static void getbanner(String tag, PnwOkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_SQUARE + "/getbanner";
            Map<String, String> params = new HashMap<>();
            params.put("os", "Android");
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
            okCallbackAdapter.setCache(tag, url, params).takeOutCache();
        }

        /**
         * 收藏/取消
         *
         * @param uid               必填 目标ID
         * @param okCallbackAdapter 响应回调
         */
        public static void collect(String tag, long uid, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_SQUARE + "/collect";
            Map<String, String> params = new HashMap<>();
            params.put("uid", String.valueOf(uid));
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }

    public static class Gift {
        static String URL_MODULE_GIFT = "/Gift";

        public static void giftList(String tag, OkCallbackAdapter okCallbackAdapter) {
            String url = URL_SERVICE + URL_MODULE_GIFT + "/giftList";
            Map<String, String> params = new HashMap<>();
            OkHttpClientManager.getInstance().post(tag, url, params, okCallbackAdapter);
        }
    }
}
